<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./main.css" />
</head>

<body>
    <div id="demo">
        <div id="small_box">
            <div id="float_box"></div>
            <img src="./1.jpg" alt="" />
        </div>
        <div id="big_box">
            <img src="./2.jpg" alt="">
        </div>
    </div>
    <script>
        window.onload = function() {
            var oDemo = document.getElementById('demo');
            var oSbox = document.getElementById('small_box');
            var ofbox = document.getElementById('float_box');
            var obbox = document.getElementById('big_box')
            var bbimg = obbox.getElementsByTagName('img')[0];

            oSbox.onmouseover = function() {
                ofbox.style.display = 'block';
                obbox.style.display = 'block';
            }

            oSbox.onmouseout = function() {
                ofbox.style.display = 'none';
                obbox.style.display = 'none';
            }

            oSbox.onmousemove = function(_ev) {
                var ev = _ev || window.event;
                var left = ev.clientX - oDemo.offsetLeft - oSbox.offsetLeft - ofbox.offsetLeft / 2;
                var top = ev.clientY - oDemo.offsetTop - oSbox.offsetTop - ofbox.offsetTop / 2;
                console.log(left);

                if (left < 0) {
                    left = 0;
                } else if (left > (oSbox.offsetWidth - ofbox.offsetWidth)) {
                    left = oSbox.offsetWidth - ofbox.offsetWidth;
                }
                if (top < 0) {
                    top = 0;
                } else if (top > (oSbox.offsetHeight - ofbox.offsetHeight)) {
                    top = oSbox.offsetHeight - ofbox.offsetHeight;
                }

                ofbox.style.left = left + 'px';
                ofbox.style.top = top + 'px';

                var percentX = left / (oSbox.offsetWidth - ofbox.offsetWidth);
                var percentY = top / (oSbox.offsetHeight - ofbox.offsetHeight);

                bbimg.style.left = -(percentX * (bbimg.offsetWidth - obbox.offsetWidth)) + 'px';
                bbimg.style.top = -(percentY * (bbimg.offsetHeight - obbox.offsetHeight)) + 'px';
            }


        }
    </script>
</body>

</html>